<?xml version="1.0" encoding="ascii"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>threadpool</title>
  <link rel="stylesheet" href="epydoc.css" type="text/css" />
  <script type="text/javascript" src="epydoc.js"></script>
</head>

<body bgcolor="white" text="black" link="blue" vlink="#204080"
      alink="#204080">
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th bgcolor="#70b0f0" class="navbar-select"
          >&nbsp;&nbsp;&nbsp;Home&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="http://chrisarndt.de/projects/threadpool/">Threadpool</a></th>
          </tr></table></th>
  </tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="100%">
      <span class="breadcrumbs">
        Module&nbsp;threadpool
      </span>
    </td>
    <td>
      <table cellpadding="0" cellspacing="0">
        <!-- hide/show private -->
        <tr><td align="right"><span class="options"
            >[<a href="frames.html" target="_top">frames</a
            >]&nbsp;|&nbsp;<a href="threadpool-pysrc.html"
            target="_top">no&nbsp;frames</a>]</span></td></tr>
      </table>
    </td>
  </tr>
</table>
<h1 class="epydoc">Source Code for <a href="threadpool-module.html">Module threadpool</a></h1>
<pre class="py-src">
<a name="L1"></a><tt class="py-lineno">  1</tt>  <tt class="py-line"><tt class="py-comment"># -*- coding: UTF-8 -*-</tt> </tt>
<a name="L2"></a><tt class="py-lineno">  2</tt>  <tt class="py-line"><tt class="py-comment"></tt><tt class="py-docstring">"""Easy to use object-oriented thread pool framework.</tt> </tt>
<a name="L3"></a><tt class="py-lineno">  3</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L4"></a><tt class="py-lineno">  4</tt>  <tt class="py-line"><tt class="py-docstring">A thread pool is an object that maintains a pool of worker threads to perform</tt> </tt>
<a name="L5"></a><tt class="py-lineno">  5</tt>  <tt class="py-line"><tt class="py-docstring">time consuming operations in parallel. It assigns jobs to the threads</tt> </tt>
<a name="L6"></a><tt class="py-lineno">  6</tt>  <tt class="py-line"><tt class="py-docstring">by putting them in a work request queue, where they are picked up by the</tt> </tt>
<a name="L7"></a><tt class="py-lineno">  7</tt>  <tt class="py-line"><tt class="py-docstring">next available thread. This then performs the requested operation in the</tt> </tt>
<a name="L8"></a><tt class="py-lineno">  8</tt>  <tt class="py-line"><tt class="py-docstring">background and puts the results in another queue.</tt> </tt>
<a name="L9"></a><tt class="py-lineno">  9</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L10"></a><tt class="py-lineno"> 10</tt>  <tt class="py-line"><tt class="py-docstring">The thread pool object can then collect the results from all threads from</tt> </tt>
<a name="L11"></a><tt class="py-lineno"> 11</tt>  <tt class="py-line"><tt class="py-docstring">this queue as soon as they become available or after all threads have</tt> </tt>
<a name="L12"></a><tt class="py-lineno"> 12</tt>  <tt class="py-line"><tt class="py-docstring">finished their work. It's also possible, to define callbacks to handle</tt> </tt>
<a name="L13"></a><tt class="py-lineno"> 13</tt>  <tt class="py-line"><tt class="py-docstring">each result as it comes in.</tt> </tt>
<a name="L14"></a><tt class="py-lineno"> 14</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L15"></a><tt class="py-lineno"> 15</tt>  <tt class="py-line"><tt class="py-docstring">The basic concept and some code was taken from the book "Python in a Nutshell,</tt> </tt>
<a name="L16"></a><tt class="py-lineno"> 16</tt>  <tt class="py-line"><tt class="py-docstring">2nd edition" by Alex Martelli, O'Reilly 2006, ISBN 0-596-10046-9, from section</tt> </tt>
<a name="L17"></a><tt class="py-lineno"> 17</tt>  <tt class="py-line"><tt class="py-docstring">14.5 "Threaded Program Architecture". I wrapped the main program logic in the</tt> </tt>
<a name="L18"></a><tt class="py-lineno"> 18</tt>  <tt class="py-line"><tt class="py-docstring">ThreadPool class, added the WorkRequest class and the callback system and</tt> </tt>
<a name="L19"></a><tt class="py-lineno"> 19</tt>  <tt class="py-line"><tt class="py-docstring">tweaked the code here and there. Kudos also to Florent Aide for the exception</tt> </tt>
<a name="L20"></a><tt class="py-lineno"> 20</tt>  <tt class="py-line"><tt class="py-docstring">handling mechanism.</tt> </tt>
<a name="L21"></a><tt class="py-lineno"> 21</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L22"></a><tt class="py-lineno"> 22</tt>  <tt class="py-line"><tt class="py-docstring">Basic usage::</tt> </tt>
<a name="L23"></a><tt class="py-lineno"> 23</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L24"></a><tt class="py-lineno"> 24</tt>  <tt class="py-line"><tt class="py-docstring">    &gt;&gt;&gt; pool = ThreadPool(poolsize)</tt> </tt>
<a name="L25"></a><tt class="py-lineno"> 25</tt>  <tt class="py-line"><tt class="py-docstring">    &gt;&gt;&gt; requests = makeRequests(some_callable, list_of_args, callback)</tt> </tt>
<a name="L26"></a><tt class="py-lineno"> 26</tt>  <tt class="py-line"><tt class="py-docstring">    &gt;&gt;&gt; [pool.putRequest(req) for req in requests]</tt> </tt>
<a name="L27"></a><tt class="py-lineno"> 27</tt>  <tt class="py-line"><tt class="py-docstring">    &gt;&gt;&gt; pool.wait()</tt> </tt>
<a name="L28"></a><tt class="py-lineno"> 28</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L29"></a><tt class="py-lineno"> 29</tt>  <tt class="py-line"><tt class="py-docstring">See the end of the module code for a brief, annotated usage example.</tt> </tt>
<a name="L30"></a><tt class="py-lineno"> 30</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L31"></a><tt class="py-lineno"> 31</tt>  <tt class="py-line"><tt class="py-docstring">Website : http://chrisarndt.de/projects/threadpool/</tt> </tt>
<a name="L32"></a><tt class="py-lineno"> 32</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L33"></a><tt class="py-lineno"> 33</tt>  <tt class="py-line"><tt class="py-docstring">"""</tt> </tt>
<a name="L34"></a><tt class="py-lineno"> 34</tt>  <tt class="py-line"><tt class="py-name">__docformat__</tt> <tt class="py-op">=</tt> <tt class="py-string">"restructuredtext en"</tt> </tt>
<a name="L35"></a><tt class="py-lineno"> 35</tt>  <tt class="py-line"> </tt>
<a name="L36"></a><tt class="py-lineno"> 36</tt>  <tt class="py-line"><tt class="py-name">__all__</tt> <tt class="py-op">=</tt> <tt class="py-op">[</tt> </tt>
<a name="L37"></a><tt class="py-lineno"> 37</tt>  <tt class="py-line">    <tt class="py-string">'makeRequests'</tt><tt class="py-op">,</tt> </tt>
<a name="L38"></a><tt class="py-lineno"> 38</tt>  <tt class="py-line">    <tt class="py-string">'NoResultsPending'</tt><tt class="py-op">,</tt> </tt>
<a name="L39"></a><tt class="py-lineno"> 39</tt>  <tt class="py-line">    <tt class="py-string">'NoWorkersAvailable'</tt><tt class="py-op">,</tt> </tt>
<a name="L40"></a><tt class="py-lineno"> 40</tt>  <tt class="py-line">    <tt class="py-string">'ThreadPool'</tt><tt class="py-op">,</tt> </tt>
<a name="L41"></a><tt class="py-lineno"> 41</tt>  <tt class="py-line">    <tt class="py-string">'WorkRequest'</tt><tt class="py-op">,</tt> </tt>
<a name="L42"></a><tt class="py-lineno"> 42</tt>  <tt class="py-line">    <tt class="py-string">'WorkerThread'</tt> </tt>
<a name="L43"></a><tt class="py-lineno"> 43</tt>  <tt class="py-line"><tt class="py-op">]</tt> </tt>
<a name="L44"></a><tt class="py-lineno"> 44</tt>  <tt class="py-line"> </tt>
<a name="L45"></a><tt class="py-lineno"> 45</tt>  <tt class="py-line"><tt class="py-name">__author__</tt> <tt class="py-op">=</tt> <tt class="py-string">"Christopher Arndt"</tt> </tt>
<a name="L46"></a><tt class="py-lineno"> 46</tt>  <tt class="py-line"><tt class="py-name">__version__</tt> <tt class="py-op">=</tt> <tt class="py-string">"1.2.5"</tt> </tt>
<a name="L47"></a><tt class="py-lineno"> 47</tt>  <tt class="py-line"><tt id="link-0" class="py-name" targets="Variable threadpool.__revision__=threadpool-module.html#__revision__"><a title="threadpool.__revision__" class="py-name" href="#" onclick="return doclink('link-0', '__revision__', 'link-0');">__revision__</a></tt> <tt class="py-op">=</tt> <tt class="py-string">"$Revision: 354 $"</tt> </tt>
<a name="L48"></a><tt class="py-lineno"> 48</tt>  <tt class="py-line"><tt class="py-name">__date__</tt> <tt class="py-op">=</tt> <tt class="py-string">"$Date: 2008-11-19 18:34:46 +0100 (Wed, 19 Nov 2008) $"</tt> </tt>
<a name="L49"></a><tt class="py-lineno"> 49</tt>  <tt class="py-line"><tt class="py-name">__license__</tt> <tt class="py-op">=</tt> <tt class="py-string">'MIT license'</tt> </tt>
<a name="L50"></a><tt class="py-lineno"> 50</tt>  <tt class="py-line"> </tt>
<a name="L51"></a><tt class="py-lineno"> 51</tt>  <tt class="py-line"> </tt>
<a name="L52"></a><tt class="py-lineno"> 52</tt>  <tt class="py-line"><tt class="py-comment"># standard library modules</tt> </tt>
<a name="L53"></a><tt class="py-lineno"> 53</tt>  <tt class="py-line"><tt class="py-comment"></tt><tt class="py-keyword">import</tt> <tt class="py-name">sys</tt> </tt>
<a name="L54"></a><tt class="py-lineno"> 54</tt>  <tt class="py-line"><tt class="py-keyword">import</tt> <tt class="py-name">threading</tt> </tt>
<a name="L55"></a><tt class="py-lineno"> 55</tt>  <tt class="py-line"><tt class="py-keyword">import</tt> <tt class="py-name">Queue</tt> </tt>
<a name="L56"></a><tt class="py-lineno"> 56</tt>  <tt class="py-line"><tt class="py-keyword">import</tt> <tt class="py-name">traceback</tt> </tt>
<a name="L57"></a><tt class="py-lineno"> 57</tt>  <tt class="py-line"> </tt>
<a name="L58"></a><tt class="py-lineno"> 58</tt>  <tt class="py-line"> </tt>
<a name="L59"></a><tt class="py-lineno"> 59</tt>  <tt class="py-line"><tt class="py-comment"># exceptions</tt> </tt>
<a name="NoResultsPending"></a><div id="NoResultsPending-def"><a name="L60"></a><tt class="py-lineno"> 60</tt> <a class="py-toggle" href="#" id="NoResultsPending-toggle" onclick="return toggle('NoResultsPending');">-</a><tt class="py-line"><tt class="py-comment"></tt><tt class="py-keyword">class</tt> <a class="py-def-name" href="threadpool.NoResultsPending-class.html">NoResultsPending</a><tt class="py-op">(</tt><tt class="py-base-class">Exception</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="NoResultsPending-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="NoResultsPending-expanded"><a name="L61"></a><tt class="py-lineno"> 61</tt>  <tt class="py-line">    <tt class="py-docstring">"""All work requests have been processed."""</tt> </tt>
<a name="L62"></a><tt class="py-lineno"> 62</tt>  <tt class="py-line">    <tt class="py-keyword">pass</tt> </tt>
</div><a name="L63"></a><tt class="py-lineno"> 63</tt>  <tt class="py-line"> </tt>
<a name="NoWorkersAvailable"></a><div id="NoWorkersAvailable-def"><a name="L64"></a><tt class="py-lineno"> 64</tt> <a class="py-toggle" href="#" id="NoWorkersAvailable-toggle" onclick="return toggle('NoWorkersAvailable');">-</a><tt class="py-line"><tt class="py-keyword">class</tt> <a class="py-def-name" href="threadpool.NoWorkersAvailable-class.html">NoWorkersAvailable</a><tt class="py-op">(</tt><tt class="py-base-class">Exception</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="NoWorkersAvailable-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="NoWorkersAvailable-expanded"><a name="L65"></a><tt class="py-lineno"> 65</tt>  <tt class="py-line">    <tt class="py-docstring">"""No worker threads available to process remaining requests."""</tt> </tt>
<a name="L66"></a><tt class="py-lineno"> 66</tt>  <tt class="py-line">    <tt class="py-keyword">pass</tt> </tt>
</div><a name="L67"></a><tt class="py-lineno"> 67</tt>  <tt class="py-line"> </tt>
<a name="L68"></a><tt class="py-lineno"> 68</tt>  <tt class="py-line"> </tt>
<a name="L69"></a><tt class="py-lineno"> 69</tt>  <tt class="py-line"><tt class="py-comment"># internal module helper functions</tt> </tt>
<a name="_handle_thread_exception"></a><div id="_handle_thread_exception-def"><a name="L70"></a><tt class="py-lineno"> 70</tt> <a class="py-toggle" href="#" id="_handle_thread_exception-toggle" onclick="return toggle('_handle_thread_exception');">-</a><tt class="py-line"><tt class="py-comment"></tt><tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool-module.html#_handle_thread_exception">_handle_thread_exception</a><tt class="py-op">(</tt><tt class="py-param">request</tt><tt class="py-op">,</tt> <tt class="py-param">exc_info</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="_handle_thread_exception-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="_handle_thread_exception-expanded"><a name="L71"></a><tt class="py-lineno"> 71</tt>  <tt class="py-line">    <tt class="py-docstring">"""Default exception handler callback function.</tt> </tt>
<a name="L72"></a><tt class="py-lineno"> 72</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L73"></a><tt class="py-lineno"> 73</tt>  <tt class="py-line"><tt class="py-docstring">    This just prints the exception info via ``traceback.print_exception``.</tt> </tt>
<a name="L74"></a><tt class="py-lineno"> 74</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L75"></a><tt class="py-lineno"> 75</tt>  <tt class="py-line"><tt class="py-docstring">    """</tt> </tt>
<a name="L76"></a><tt class="py-lineno"> 76</tt>  <tt class="py-line">    <tt class="py-name">traceback</tt><tt class="py-op">.</tt><tt class="py-name">print_exception</tt><tt class="py-op">(</tt><tt class="py-op">*</tt><tt class="py-name">exc_info</tt><tt class="py-op">)</tt> </tt>
</div><a name="L77"></a><tt class="py-lineno"> 77</tt>  <tt class="py-line"> </tt>
<a name="L78"></a><tt class="py-lineno"> 78</tt>  <tt class="py-line"> </tt>
<a name="L79"></a><tt class="py-lineno"> 79</tt>  <tt class="py-line"><tt class="py-comment"># utility functions</tt> </tt>
<a name="makeRequests"></a><div id="makeRequests-def"><a name="L80"></a><tt class="py-lineno"> 80</tt> <a class="py-toggle" href="#" id="makeRequests-toggle" onclick="return toggle('makeRequests');">-</a><tt class="py-line"><tt class="py-comment"></tt><tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool-module.html#makeRequests">makeRequests</a><tt class="py-op">(</tt><tt class="py-param">callable_</tt><tt class="py-op">,</tt> <tt class="py-param">args_list</tt><tt class="py-op">,</tt> <tt class="py-param">callback</tt><tt class="py-op">=</tt><tt class="py-name">None</tt><tt class="py-op">,</tt> </tt>
<a name="L81"></a><tt class="py-lineno"> 81</tt>  <tt class="py-line">        <tt class="py-param">exc_callback</tt><tt class="py-op">=</tt><tt class="py-name">_handle_thread_exception</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="makeRequests-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="makeRequests-expanded"><a name="L82"></a><tt class="py-lineno"> 82</tt>  <tt class="py-line">    <tt class="py-docstring">"""Create several work requests for same callable with different arguments.</tt> </tt>
<a name="L83"></a><tt class="py-lineno"> 83</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L84"></a><tt class="py-lineno"> 84</tt>  <tt class="py-line"><tt class="py-docstring">    Convenience function for creating several work requests for the same</tt> </tt>
<a name="L85"></a><tt class="py-lineno"> 85</tt>  <tt class="py-line"><tt class="py-docstring">    callable where each invocation of the callable receives different values</tt> </tt>
<a name="L86"></a><tt class="py-lineno"> 86</tt>  <tt class="py-line"><tt class="py-docstring">    for its arguments.</tt> </tt>
<a name="L87"></a><tt class="py-lineno"> 87</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L88"></a><tt class="py-lineno"> 88</tt>  <tt class="py-line"><tt class="py-docstring">    ``args_list`` contains the parameters for each invocation of callable.</tt> </tt>
<a name="L89"></a><tt class="py-lineno"> 89</tt>  <tt class="py-line"><tt class="py-docstring">    Each item in ``args_list`` should be either a 2-item tuple of the list of</tt> </tt>
<a name="L90"></a><tt class="py-lineno"> 90</tt>  <tt class="py-line"><tt class="py-docstring">    positional arguments and a dictionary of keyword arguments or a single,</tt> </tt>
<a name="L91"></a><tt class="py-lineno"> 91</tt>  <tt class="py-line"><tt class="py-docstring">    non-tuple argument.</tt> </tt>
<a name="L92"></a><tt class="py-lineno"> 92</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L93"></a><tt class="py-lineno"> 93</tt>  <tt class="py-line"><tt class="py-docstring">    See docstring for ``WorkRequest`` for info on ``callback`` and</tt> </tt>
<a name="L94"></a><tt class="py-lineno"> 94</tt>  <tt class="py-line"><tt class="py-docstring">    ``exc_callback``.</tt> </tt>
<a name="L95"></a><tt class="py-lineno"> 95</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L96"></a><tt class="py-lineno"> 96</tt>  <tt class="py-line"><tt class="py-docstring">    """</tt> </tt>
<a name="L97"></a><tt class="py-lineno"> 97</tt>  <tt class="py-line">    <tt class="py-name">requests</tt> <tt class="py-op">=</tt> <tt class="py-op">[</tt><tt class="py-op">]</tt> </tt>
<a name="L98"></a><tt class="py-lineno"> 98</tt>  <tt class="py-line">    <tt class="py-keyword">for</tt> <tt class="py-name">item</tt> <tt class="py-keyword">in</tt> <tt class="py-name">args_list</tt><tt class="py-op">:</tt> </tt>
<a name="L99"></a><tt class="py-lineno"> 99</tt>  <tt class="py-line">        <tt class="py-keyword">if</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">item</tt><tt class="py-op">,</tt> <tt class="py-name">tuple</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L100"></a><tt class="py-lineno">100</tt>  <tt class="py-line">            <tt class="py-name">requests</tt><tt class="py-op">.</tt><tt class="py-name">append</tt><tt class="py-op">(</tt> </tt>
<a name="L101"></a><tt class="py-lineno">101</tt>  <tt class="py-line">                <tt id="link-1" class="py-name" targets="Class threadpool.WorkRequest=threadpool.WorkRequest-class.html"><a title="threadpool.WorkRequest" class="py-name" href="#" onclick="return doclink('link-1', 'WorkRequest', 'link-1');">WorkRequest</a></tt><tt class="py-op">(</tt><tt class="py-name">callable_</tt><tt class="py-op">,</tt> <tt class="py-name">item</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt><tt class="py-op">,</tt> <tt class="py-name">item</tt><tt class="py-op">[</tt><tt class="py-number">1</tt><tt class="py-op">]</tt><tt class="py-op">,</tt> <tt class="py-name">callback</tt><tt class="py-op">=</tt><tt class="py-name">callback</tt><tt class="py-op">,</tt> </tt>
<a name="L102"></a><tt class="py-lineno">102</tt>  <tt class="py-line">                    <tt class="py-name">exc_callback</tt><tt class="py-op">=</tt><tt class="py-name">exc_callback</tt><tt class="py-op">)</tt> </tt>
<a name="L103"></a><tt class="py-lineno">103</tt>  <tt class="py-line">            <tt class="py-op">)</tt> </tt>
<a name="L104"></a><tt class="py-lineno">104</tt>  <tt class="py-line">        <tt class="py-keyword">else</tt><tt class="py-op">:</tt> </tt>
<a name="L105"></a><tt class="py-lineno">105</tt>  <tt class="py-line">            <tt class="py-name">requests</tt><tt class="py-op">.</tt><tt class="py-name">append</tt><tt class="py-op">(</tt> </tt>
<a name="L106"></a><tt class="py-lineno">106</tt>  <tt class="py-line">                <tt id="link-2" class="py-name"><a title="threadpool.WorkRequest" class="py-name" href="#" onclick="return doclink('link-2', 'WorkRequest', 'link-1');">WorkRequest</a></tt><tt class="py-op">(</tt><tt class="py-name">callable_</tt><tt class="py-op">,</tt> <tt class="py-op">[</tt><tt class="py-name">item</tt><tt class="py-op">]</tt><tt class="py-op">,</tt> <tt class="py-name">None</tt><tt class="py-op">,</tt> <tt class="py-name">callback</tt><tt class="py-op">=</tt><tt class="py-name">callback</tt><tt class="py-op">,</tt> </tt>
<a name="L107"></a><tt class="py-lineno">107</tt>  <tt class="py-line">                    <tt class="py-name">exc_callback</tt><tt class="py-op">=</tt><tt class="py-name">exc_callback</tt><tt class="py-op">)</tt> </tt>
<a name="L108"></a><tt class="py-lineno">108</tt>  <tt class="py-line">            <tt class="py-op">)</tt> </tt>
<a name="L109"></a><tt class="py-lineno">109</tt>  <tt class="py-line">    <tt class="py-keyword">return</tt> <tt class="py-name">requests</tt> </tt>
</div><a name="L110"></a><tt class="py-lineno">110</tt>  <tt class="py-line"> </tt>
<a name="L111"></a><tt class="py-lineno">111</tt>  <tt class="py-line"> </tt>
<a name="L112"></a><tt class="py-lineno">112</tt>  <tt class="py-line"><tt class="py-comment"># classes</tt> </tt>
<a name="WorkerThread"></a><div id="WorkerThread-def"><a name="L113"></a><tt class="py-lineno">113</tt> <a class="py-toggle" href="#" id="WorkerThread-toggle" onclick="return toggle('WorkerThread');">-</a><tt class="py-line"><tt class="py-comment"></tt><tt class="py-keyword">class</tt> <a class="py-def-name" href="threadpool.WorkerThread-class.html">WorkerThread</a><tt class="py-op">(</tt><tt class="py-base-class">threading</tt><tt class="py-op">.</tt><tt class="py-base-class">Thread</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="WorkerThread-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="WorkerThread-expanded"><a name="L114"></a><tt class="py-lineno">114</tt>  <tt class="py-line">    <tt class="py-docstring">"""Background thread connected to the requests/results queues.</tt> </tt>
<a name="L115"></a><tt class="py-lineno">115</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L116"></a><tt class="py-lineno">116</tt>  <tt class="py-line"><tt class="py-docstring">    A worker thread sits in the background and picks up work requests from</tt> </tt>
<a name="L117"></a><tt class="py-lineno">117</tt>  <tt class="py-line"><tt class="py-docstring">    one queue and puts the results in another until it is dismissed.</tt> </tt>
<a name="L118"></a><tt class="py-lineno">118</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L119"></a><tt class="py-lineno">119</tt>  <tt class="py-line"><tt class="py-docstring">    """</tt> </tt>
<a name="L120"></a><tt class="py-lineno">120</tt>  <tt class="py-line"> </tt>
<a name="WorkerThread.__init__"></a><div id="WorkerThread.__init__-def"><a name="L121"></a><tt class="py-lineno">121</tt> <a class="py-toggle" href="#" id="WorkerThread.__init__-toggle" onclick="return toggle('WorkerThread.__init__');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool.WorkerThread-class.html#__init__">__init__</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">requests_queue</tt><tt class="py-op">,</tt> <tt class="py-param">results_queue</tt><tt class="py-op">,</tt> <tt class="py-param">poll_timeout</tt><tt class="py-op">=</tt><tt class="py-number">5</tt><tt class="py-op">,</tt> <tt class="py-op">**</tt><tt class="py-param">kwds</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="WorkerThread.__init__-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="WorkerThread.__init__-expanded"><a name="L122"></a><tt class="py-lineno">122</tt>  <tt class="py-line">        <tt class="py-docstring">"""Set up thread in daemonic mode and start it immediatedly.</tt> </tt>
<a name="L123"></a><tt class="py-lineno">123</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L124"></a><tt class="py-lineno">124</tt>  <tt class="py-line"><tt class="py-docstring">        ``requests_queue`` and ``results_queue`` are instances of</tt> </tt>
<a name="L125"></a><tt class="py-lineno">125</tt>  <tt class="py-line"><tt class="py-docstring">        ``Queue.Queue`` passed by the ``ThreadPool`` class when it creates a new</tt> </tt>
<a name="L126"></a><tt class="py-lineno">126</tt>  <tt class="py-line"><tt class="py-docstring">        worker thread.</tt> </tt>
<a name="L127"></a><tt class="py-lineno">127</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L128"></a><tt class="py-lineno">128</tt>  <tt class="py-line"><tt class="py-docstring">        """</tt> </tt>
<a name="L129"></a><tt class="py-lineno">129</tt>  <tt class="py-line">        <tt class="py-name">threading</tt><tt class="py-op">.</tt><tt class="py-name">Thread</tt><tt class="py-op">.</tt><tt id="link-3" class="py-name" targets="Method threadpool.ThreadPool.__init__()=threadpool.ThreadPool-class.html#__init__,Method threadpool.WorkRequest.__init__()=threadpool.WorkRequest-class.html#__init__,Method threadpool.WorkerThread.__init__()=threadpool.WorkerThread-class.html#__init__"><a title="threadpool.ThreadPool.__init__
threadpool.WorkRequest.__init__
threadpool.WorkerThread.__init__" class="py-name" href="#" onclick="return doclink('link-3', '__init__', 'link-3');">__init__</a></tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">,</tt> <tt class="py-op">**</tt><tt class="py-name">kwds</tt><tt class="py-op">)</tt> </tt>
<a name="L130"></a><tt class="py-lineno">130</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">setDaemon</tt><tt class="py-op">(</tt><tt class="py-number">1</tt><tt class="py-op">)</tt> </tt>
<a name="L131"></a><tt class="py-lineno">131</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_requests_queue</tt> <tt class="py-op">=</tt> <tt class="py-name">requests_queue</tt> </tt>
<a name="L132"></a><tt class="py-lineno">132</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_results_queue</tt> <tt class="py-op">=</tt> <tt class="py-name">results_queue</tt> </tt>
<a name="L133"></a><tt class="py-lineno">133</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_poll_timeout</tt> <tt class="py-op">=</tt> <tt class="py-name">poll_timeout</tt> </tt>
<a name="L134"></a><tt class="py-lineno">134</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_dismissed</tt> <tt class="py-op">=</tt> <tt class="py-name">threading</tt><tt class="py-op">.</tt><tt class="py-name">Event</tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
<a name="L135"></a><tt class="py-lineno">135</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">start</tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
</div><a name="L136"></a><tt class="py-lineno">136</tt>  <tt class="py-line"> </tt>
<a name="WorkerThread.run"></a><div id="WorkerThread.run-def"><a name="L137"></a><tt class="py-lineno">137</tt> <a class="py-toggle" href="#" id="WorkerThread.run-toggle" onclick="return toggle('WorkerThread.run');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool.WorkerThread-class.html#run">run</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="WorkerThread.run-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="WorkerThread.run-expanded"><a name="L138"></a><tt class="py-lineno">138</tt>  <tt class="py-line">        <tt class="py-docstring">"""Repeatedly process the job queue until told to exit."""</tt> </tt>
<a name="L139"></a><tt class="py-lineno">139</tt>  <tt class="py-line">        <tt class="py-keyword">while</tt> <tt class="py-name">True</tt><tt class="py-op">:</tt> </tt>
<a name="L140"></a><tt class="py-lineno">140</tt>  <tt class="py-line">            <tt class="py-keyword">if</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_dismissed</tt><tt class="py-op">.</tt><tt class="py-name">isSet</tt><tt class="py-op">(</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L141"></a><tt class="py-lineno">141</tt>  <tt class="py-line">                <tt class="py-comment"># we are dismissed, break out of loop</tt> </tt>
<a name="L142"></a><tt class="py-lineno">142</tt>  <tt class="py-line"><tt class="py-comment"></tt>                <tt class="py-keyword">break</tt> </tt>
<a name="L143"></a><tt class="py-lineno">143</tt>  <tt class="py-line">            <tt class="py-comment"># get next work request. If we don't get a new request from the</tt> </tt>
<a name="L144"></a><tt class="py-lineno">144</tt>  <tt class="py-line"><tt class="py-comment"></tt>            <tt class="py-comment"># queue after self._poll_timout seconds, we jump to the start of</tt> </tt>
<a name="L145"></a><tt class="py-lineno">145</tt>  <tt class="py-line"><tt class="py-comment"></tt>            <tt class="py-comment"># the while loop again, to give the thread a chance to exit.</tt> </tt>
<a name="L146"></a><tt class="py-lineno">146</tt>  <tt class="py-line"><tt class="py-comment"></tt>            <tt class="py-keyword">try</tt><tt class="py-op">:</tt> </tt>
<a name="L147"></a><tt class="py-lineno">147</tt>  <tt class="py-line">                <tt class="py-name">request</tt> <tt class="py-op">=</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_requests_queue</tt><tt class="py-op">.</tt><tt class="py-name">get</tt><tt class="py-op">(</tt><tt class="py-name">True</tt><tt class="py-op">,</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_poll_timeout</tt><tt class="py-op">)</tt> </tt>
<a name="L148"></a><tt class="py-lineno">148</tt>  <tt class="py-line">            <tt class="py-keyword">except</tt> <tt class="py-name">Queue</tt><tt class="py-op">.</tt><tt class="py-name">Empty</tt><tt class="py-op">:</tt> </tt>
<a name="L149"></a><tt class="py-lineno">149</tt>  <tt class="py-line">                <tt class="py-keyword">continue</tt> </tt>
<a name="L150"></a><tt class="py-lineno">150</tt>  <tt class="py-line">            <tt class="py-keyword">else</tt><tt class="py-op">:</tt> </tt>
<a name="L151"></a><tt class="py-lineno">151</tt>  <tt class="py-line">                <tt class="py-keyword">if</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_dismissed</tt><tt class="py-op">.</tt><tt class="py-name">isSet</tt><tt class="py-op">(</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L152"></a><tt class="py-lineno">152</tt>  <tt class="py-line">                    <tt class="py-comment"># we are dismissed, put back request in queue and exit loop</tt> </tt>
<a name="L153"></a><tt class="py-lineno">153</tt>  <tt class="py-line"><tt class="py-comment"></tt>                    <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_requests_queue</tt><tt class="py-op">.</tt><tt class="py-name">put</tt><tt class="py-op">(</tt><tt class="py-name">request</tt><tt class="py-op">)</tt> </tt>
<a name="L154"></a><tt class="py-lineno">154</tt>  <tt class="py-line">                    <tt class="py-keyword">break</tt> </tt>
<a name="L155"></a><tt class="py-lineno">155</tt>  <tt class="py-line">                <tt class="py-keyword">try</tt><tt class="py-op">:</tt> </tt>
<a name="L156"></a><tt class="py-lineno">156</tt>  <tt class="py-line">                    <tt class="py-name">result</tt> <tt class="py-op">=</tt> <tt class="py-name">request</tt><tt class="py-op">.</tt><tt class="py-name">callable</tt><tt class="py-op">(</tt><tt class="py-op">*</tt><tt class="py-name">request</tt><tt class="py-op">.</tt><tt class="py-name">args</tt><tt class="py-op">,</tt> <tt class="py-op">**</tt><tt class="py-name">request</tt><tt class="py-op">.</tt><tt class="py-name">kwds</tt><tt class="py-op">)</tt> </tt>
<a name="L157"></a><tt class="py-lineno">157</tt>  <tt class="py-line">                    <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_results_queue</tt><tt class="py-op">.</tt><tt class="py-name">put</tt><tt class="py-op">(</tt><tt class="py-op">(</tt><tt class="py-name">request</tt><tt class="py-op">,</tt> <tt class="py-name">result</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> </tt>
<a name="L158"></a><tt class="py-lineno">158</tt>  <tt class="py-line">                <tt class="py-keyword">except</tt><tt class="py-op">:</tt> </tt>
<a name="L159"></a><tt class="py-lineno">159</tt>  <tt class="py-line">                    <tt class="py-name">request</tt><tt class="py-op">.</tt><tt class="py-name">exception</tt> <tt class="py-op">=</tt> <tt class="py-name">True</tt> </tt>
<a name="L160"></a><tt class="py-lineno">160</tt>  <tt class="py-line">                    <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_results_queue</tt><tt class="py-op">.</tt><tt class="py-name">put</tt><tt class="py-op">(</tt><tt class="py-op">(</tt><tt class="py-name">request</tt><tt class="py-op">,</tt> <tt class="py-name">sys</tt><tt class="py-op">.</tt><tt class="py-name">exc_info</tt><tt class="py-op">(</tt><tt class="py-op">)</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> </tt>
</div><a name="L161"></a><tt class="py-lineno">161</tt>  <tt class="py-line"> </tt>
<a name="WorkerThread.dismiss"></a><div id="WorkerThread.dismiss-def"><a name="L162"></a><tt class="py-lineno">162</tt> <a class="py-toggle" href="#" id="WorkerThread.dismiss-toggle" onclick="return toggle('WorkerThread.dismiss');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool.WorkerThread-class.html#dismiss">dismiss</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="WorkerThread.dismiss-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="WorkerThread.dismiss-expanded"><a name="L163"></a><tt class="py-lineno">163</tt>  <tt class="py-line">        <tt class="py-docstring">"""Sets a flag to tell the thread to exit when done with current job."""</tt> </tt>
<a name="L164"></a><tt class="py-lineno">164</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_dismissed</tt><tt class="py-op">.</tt><tt class="py-name">set</tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
</div></div><a name="L165"></a><tt class="py-lineno">165</tt>  <tt class="py-line"> </tt>
<a name="L166"></a><tt class="py-lineno">166</tt>  <tt class="py-line"> </tt>
<a name="WorkRequest"></a><div id="WorkRequest-def"><a name="L167"></a><tt class="py-lineno">167</tt> <a class="py-toggle" href="#" id="WorkRequest-toggle" onclick="return toggle('WorkRequest');">-</a><tt class="py-line"><tt class="py-keyword">class</tt> <a class="py-def-name" href="threadpool.WorkRequest-class.html">WorkRequest</a><tt class="py-op">:</tt> </tt>
</div><div id="WorkRequest-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="WorkRequest-expanded"><a name="L168"></a><tt class="py-lineno">168</tt>  <tt class="py-line">    <tt class="py-docstring">"""A request to execute a callable for putting in the request queue later.</tt> </tt>
<a name="L169"></a><tt class="py-lineno">169</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L170"></a><tt class="py-lineno">170</tt>  <tt class="py-line"><tt class="py-docstring">    See the module function ``makeRequests`` for the common case</tt> </tt>
<a name="L171"></a><tt class="py-lineno">171</tt>  <tt class="py-line"><tt class="py-docstring">    where you want to build several ``WorkRequest`` objects for the same</tt> </tt>
<a name="L172"></a><tt class="py-lineno">172</tt>  <tt class="py-line"><tt class="py-docstring">    callable but with different arguments for each call.</tt> </tt>
<a name="L173"></a><tt class="py-lineno">173</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L174"></a><tt class="py-lineno">174</tt>  <tt class="py-line"><tt class="py-docstring">    """</tt> </tt>
<a name="L175"></a><tt class="py-lineno">175</tt>  <tt class="py-line"> </tt>
<a name="WorkRequest.__init__"></a><div id="WorkRequest.__init__-def"><a name="L176"></a><tt class="py-lineno">176</tt> <a class="py-toggle" href="#" id="WorkRequest.__init__-toggle" onclick="return toggle('WorkRequest.__init__');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool.WorkRequest-class.html#__init__">__init__</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">callable_</tt><tt class="py-op">,</tt> <tt class="py-param">args</tt><tt class="py-op">=</tt><tt class="py-name">None</tt><tt class="py-op">,</tt> <tt class="py-param">kwds</tt><tt class="py-op">=</tt><tt class="py-name">None</tt><tt class="py-op">,</tt> <tt class="py-param">requestID</tt><tt class="py-op">=</tt><tt class="py-name">None</tt><tt class="py-op">,</tt> </tt>
<a name="L177"></a><tt class="py-lineno">177</tt>  <tt class="py-line">            <tt class="py-param">callback</tt><tt class="py-op">=</tt><tt class="py-name">None</tt><tt class="py-op">,</tt> <tt class="py-param">exc_callback</tt><tt class="py-op">=</tt><tt class="py-name">_handle_thread_exception</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="WorkRequest.__init__-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="WorkRequest.__init__-expanded"><a name="L178"></a><tt class="py-lineno">178</tt>  <tt class="py-line">        <tt class="py-docstring">"""Create a work request for a callable and attach callbacks.</tt> </tt>
<a name="L179"></a><tt class="py-lineno">179</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L180"></a><tt class="py-lineno">180</tt>  <tt class="py-line"><tt class="py-docstring">        A work request consists of the a callable to be executed by a</tt> </tt>
<a name="L181"></a><tt class="py-lineno">181</tt>  <tt class="py-line"><tt class="py-docstring">        worker thread, a list of positional arguments, a dictionary</tt> </tt>
<a name="L182"></a><tt class="py-lineno">182</tt>  <tt class="py-line"><tt class="py-docstring">        of keyword arguments.</tt> </tt>
<a name="L183"></a><tt class="py-lineno">183</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L184"></a><tt class="py-lineno">184</tt>  <tt class="py-line"><tt class="py-docstring">        A ``callback`` function can be specified, that is called when the</tt> </tt>
<a name="L185"></a><tt class="py-lineno">185</tt>  <tt class="py-line"><tt class="py-docstring">        results of the request are picked up from the result queue. It must</tt> </tt>
<a name="L186"></a><tt class="py-lineno">186</tt>  <tt class="py-line"><tt class="py-docstring">        accept two anonymous arguments, the ``WorkRequest`` object and the</tt> </tt>
<a name="L187"></a><tt class="py-lineno">187</tt>  <tt class="py-line"><tt class="py-docstring">        results of the callable, in that order. If you want to pass additional</tt> </tt>
<a name="L188"></a><tt class="py-lineno">188</tt>  <tt class="py-line"><tt class="py-docstring">        information to the callback, just stick it on the request object.</tt> </tt>
<a name="L189"></a><tt class="py-lineno">189</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L190"></a><tt class="py-lineno">190</tt>  <tt class="py-line"><tt class="py-docstring">        You can also give custom callback for when an exception occurs with</tt> </tt>
<a name="L191"></a><tt class="py-lineno">191</tt>  <tt class="py-line"><tt class="py-docstring">        the ``exc_callback`` keyword parameter. It should also accept two</tt> </tt>
<a name="L192"></a><tt class="py-lineno">192</tt>  <tt class="py-line"><tt class="py-docstring">        anonymous arguments, the ``WorkRequest`` and a tuple with the exception</tt> </tt>
<a name="L193"></a><tt class="py-lineno">193</tt>  <tt class="py-line"><tt class="py-docstring">        details as returned by ``sys.exc_info()``. The default implementation</tt> </tt>
<a name="L194"></a><tt class="py-lineno">194</tt>  <tt class="py-line"><tt class="py-docstring">        of this callback just prints the exception info via</tt> </tt>
<a name="L195"></a><tt class="py-lineno">195</tt>  <tt class="py-line"><tt class="py-docstring">        ``traceback.print_exception``. If you want no exception handler</tt> </tt>
<a name="L196"></a><tt class="py-lineno">196</tt>  <tt class="py-line"><tt class="py-docstring">        callback, just pass in ``None``.</tt> </tt>
<a name="L197"></a><tt class="py-lineno">197</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L198"></a><tt class="py-lineno">198</tt>  <tt class="py-line"><tt class="py-docstring">        ``requestID``, if given, must be hashable since it is used by</tt> </tt>
<a name="L199"></a><tt class="py-lineno">199</tt>  <tt class="py-line"><tt class="py-docstring">        ``ThreadPool`` object to store the results of that work request in a</tt> </tt>
<a name="L200"></a><tt class="py-lineno">200</tt>  <tt class="py-line"><tt class="py-docstring">        dictionary. It defaults to the return value of ``id(self)``.</tt> </tt>
<a name="L201"></a><tt class="py-lineno">201</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L202"></a><tt class="py-lineno">202</tt>  <tt class="py-line"><tt class="py-docstring">        """</tt> </tt>
<a name="L203"></a><tt class="py-lineno">203</tt>  <tt class="py-line">        <tt class="py-keyword">if</tt> <tt class="py-name">requestID</tt> <tt class="py-keyword">is</tt> <tt class="py-name">None</tt><tt class="py-op">:</tt> </tt>
<a name="L204"></a><tt class="py-lineno">204</tt>  <tt class="py-line">            <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">requestID</tt> <tt class="py-op">=</tt> <tt class="py-name">id</tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">)</tt> </tt>
<a name="L205"></a><tt class="py-lineno">205</tt>  <tt class="py-line">        <tt class="py-keyword">else</tt><tt class="py-op">:</tt> </tt>
<a name="L206"></a><tt class="py-lineno">206</tt>  <tt class="py-line">            <tt class="py-keyword">try</tt><tt class="py-op">:</tt> </tt>
<a name="L207"></a><tt class="py-lineno">207</tt>  <tt class="py-line">                <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">requestID</tt> <tt class="py-op">=</tt> <tt class="py-name">hash</tt><tt class="py-op">(</tt><tt class="py-name">requestID</tt><tt class="py-op">)</tt> </tt>
<a name="L208"></a><tt class="py-lineno">208</tt>  <tt class="py-line">            <tt class="py-keyword">except</tt> <tt class="py-name">TypeError</tt><tt class="py-op">:</tt> </tt>
<a name="L209"></a><tt class="py-lineno">209</tt>  <tt class="py-line">                <tt class="py-keyword">raise</tt> <tt class="py-name">TypeError</tt><tt class="py-op">(</tt><tt class="py-string">"requestID must be hashable."</tt><tt class="py-op">)</tt> </tt>
<a name="L210"></a><tt class="py-lineno">210</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">exception</tt> <tt class="py-op">=</tt> <tt class="py-name">False</tt> </tt>
<a name="L211"></a><tt class="py-lineno">211</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">callback</tt> <tt class="py-op">=</tt> <tt class="py-name">callback</tt> </tt>
<a name="L212"></a><tt class="py-lineno">212</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">exc_callback</tt> <tt class="py-op">=</tt> <tt class="py-name">exc_callback</tt> </tt>
<a name="L213"></a><tt class="py-lineno">213</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">callable</tt> <tt class="py-op">=</tt> <tt class="py-name">callable_</tt> </tt>
<a name="L214"></a><tt class="py-lineno">214</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">args</tt> <tt class="py-op">=</tt> <tt class="py-name">args</tt> <tt class="py-keyword">or</tt> <tt class="py-op">[</tt><tt class="py-op">]</tt> </tt>
<a name="L215"></a><tt class="py-lineno">215</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">kwds</tt> <tt class="py-op">=</tt> <tt class="py-name">kwds</tt> <tt class="py-keyword">or</tt> <tt class="py-op">{</tt><tt class="py-op">}</tt> </tt>
</div><a name="L216"></a><tt class="py-lineno">216</tt>  <tt class="py-line"> </tt>
<a name="WorkRequest.__str__"></a><div id="WorkRequest.__str__-def"><a name="L217"></a><tt class="py-lineno">217</tt> <a class="py-toggle" href="#" id="WorkRequest.__str__-toggle" onclick="return toggle('WorkRequest.__str__');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool.WorkRequest-class.html#__str__">__str__</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="WorkRequest.__str__-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="WorkRequest.__str__-expanded"><a name="L218"></a><tt class="py-lineno">218</tt>  <tt class="py-line">        <tt class="py-keyword">return</tt> <tt class="py-string">"&lt;WorkRequest id=%s args=%r kwargs=%r exception=%s&gt;"</tt> <tt class="py-op">%</tt> \ </tt>
<a name="L219"></a><tt class="py-lineno">219</tt>  <tt class="py-line">            <tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">requestID</tt><tt class="py-op">,</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">args</tt><tt class="py-op">,</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">kwds</tt><tt class="py-op">,</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">exception</tt><tt class="py-op">)</tt> </tt>
</div></div><a name="L220"></a><tt class="py-lineno">220</tt>  <tt class="py-line"> </tt>
<a name="ThreadPool"></a><div id="ThreadPool-def"><a name="L221"></a><tt class="py-lineno">221</tt> <a class="py-toggle" href="#" id="ThreadPool-toggle" onclick="return toggle('ThreadPool');">-</a><tt class="py-line"><tt class="py-keyword">class</tt> <a class="py-def-name" href="threadpool.ThreadPool-class.html">ThreadPool</a><tt class="py-op">:</tt> </tt>
</div><div id="ThreadPool-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="ThreadPool-expanded"><a name="L222"></a><tt class="py-lineno">222</tt>  <tt class="py-line">    <tt class="py-docstring">"""A thread pool, distributing work requests and collecting results.</tt> </tt>
<a name="L223"></a><tt class="py-lineno">223</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L224"></a><tt class="py-lineno">224</tt>  <tt class="py-line"><tt class="py-docstring">    See the module docstring for more information.</tt> </tt>
<a name="L225"></a><tt class="py-lineno">225</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L226"></a><tt class="py-lineno">226</tt>  <tt class="py-line"><tt class="py-docstring">    """</tt> </tt>
<a name="L227"></a><tt class="py-lineno">227</tt>  <tt class="py-line"> </tt>
<a name="ThreadPool.__init__"></a><div id="ThreadPool.__init__-def"><a name="L228"></a><tt class="py-lineno">228</tt> <a class="py-toggle" href="#" id="ThreadPool.__init__-toggle" onclick="return toggle('ThreadPool.__init__');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool.ThreadPool-class.html#__init__">__init__</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">num_workers</tt><tt class="py-op">,</tt> <tt class="py-param">q_size</tt><tt class="py-op">=</tt><tt class="py-number">0</tt><tt class="py-op">,</tt> <tt class="py-param">resq_size</tt><tt class="py-op">=</tt><tt class="py-number">0</tt><tt class="py-op">,</tt> <tt class="py-param">poll_timeout</tt><tt class="py-op">=</tt><tt class="py-number">5</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="ThreadPool.__init__-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="ThreadPool.__init__-expanded"><a name="L229"></a><tt class="py-lineno">229</tt>  <tt class="py-line">        <tt class="py-docstring">"""Set up the thread pool and start num_workers worker threads.</tt> </tt>
<a name="L230"></a><tt class="py-lineno">230</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L231"></a><tt class="py-lineno">231</tt>  <tt class="py-line"><tt class="py-docstring">        ``num_workers`` is the number of worker threads to start initially.</tt> </tt>
<a name="L232"></a><tt class="py-lineno">232</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L233"></a><tt class="py-lineno">233</tt>  <tt class="py-line"><tt class="py-docstring">        If ``q_size &gt; 0`` the size of the work *request queue* is limited and</tt> </tt>
<a name="L234"></a><tt class="py-lineno">234</tt>  <tt class="py-line"><tt class="py-docstring">        the thread pool blocks when the queue is full and it tries to put</tt> </tt>
<a name="L235"></a><tt class="py-lineno">235</tt>  <tt class="py-line"><tt class="py-docstring">        more work requests in it (see ``putRequest`` method), unless you also</tt> </tt>
<a name="L236"></a><tt class="py-lineno">236</tt>  <tt class="py-line"><tt class="py-docstring">        use a positive ``timeout`` value for ``putRequest``.</tt> </tt>
<a name="L237"></a><tt class="py-lineno">237</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L238"></a><tt class="py-lineno">238</tt>  <tt class="py-line"><tt class="py-docstring">        If ``resq_size &gt; 0`` the size of the *results queue* is limited and the</tt> </tt>
<a name="L239"></a><tt class="py-lineno">239</tt>  <tt class="py-line"><tt class="py-docstring">        worker threads will block when the queue is full and they try to put</tt> </tt>
<a name="L240"></a><tt class="py-lineno">240</tt>  <tt class="py-line"><tt class="py-docstring">        new results in it.</tt> </tt>
<a name="L241"></a><tt class="py-lineno">241</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L242"></a><tt class="py-lineno">242</tt>  <tt class="py-line"><tt class="py-docstring">        .. warning:</tt> </tt>
<a name="L243"></a><tt class="py-lineno">243</tt>  <tt class="py-line"><tt class="py-docstring">            If you set both ``q_size`` and ``resq_size`` to ``!= 0`` there is</tt> </tt>
<a name="L244"></a><tt class="py-lineno">244</tt>  <tt class="py-line"><tt class="py-docstring">            the possibilty of a deadlock, when the results queue is not pulled</tt> </tt>
<a name="L245"></a><tt class="py-lineno">245</tt>  <tt class="py-line"><tt class="py-docstring">            regularly and too many jobs are put in the work requests queue.</tt> </tt>
<a name="L246"></a><tt class="py-lineno">246</tt>  <tt class="py-line"><tt class="py-docstring">            To prevent this, always set ``timeout &gt; 0`` when calling</tt> </tt>
<a name="L247"></a><tt class="py-lineno">247</tt>  <tt class="py-line"><tt class="py-docstring">            ``ThreadPool.putRequest()`` and catch ``Queue.Full`` exceptions.</tt> </tt>
<a name="L248"></a><tt class="py-lineno">248</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L249"></a><tt class="py-lineno">249</tt>  <tt class="py-line"><tt class="py-docstring">        """</tt> </tt>
<a name="L250"></a><tt class="py-lineno">250</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_requests_queue</tt> <tt class="py-op">=</tt> <tt class="py-name">Queue</tt><tt class="py-op">.</tt><tt class="py-name">Queue</tt><tt class="py-op">(</tt><tt class="py-name">q_size</tt><tt class="py-op">)</tt> </tt>
<a name="L251"></a><tt class="py-lineno">251</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_results_queue</tt> <tt class="py-op">=</tt> <tt class="py-name">Queue</tt><tt class="py-op">.</tt><tt class="py-name">Queue</tt><tt class="py-op">(</tt><tt class="py-name">resq_size</tt><tt class="py-op">)</tt> </tt>
<a name="L252"></a><tt class="py-lineno">252</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">workers</tt> <tt class="py-op">=</tt> <tt class="py-op">[</tt><tt class="py-op">]</tt> </tt>
<a name="L253"></a><tt class="py-lineno">253</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">dismissedWorkers</tt> <tt class="py-op">=</tt> <tt class="py-op">[</tt><tt class="py-op">]</tt> </tt>
<a name="L254"></a><tt class="py-lineno">254</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">workRequests</tt> <tt class="py-op">=</tt> <tt class="py-op">{</tt><tt class="py-op">}</tt> </tt>
<a name="L255"></a><tt class="py-lineno">255</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-4" class="py-name" targets="Method threadpool.ThreadPool.createWorkers()=threadpool.ThreadPool-class.html#createWorkers"><a title="threadpool.ThreadPool.createWorkers" class="py-name" href="#" onclick="return doclink('link-4', 'createWorkers', 'link-4');">createWorkers</a></tt><tt class="py-op">(</tt><tt class="py-name">num_workers</tt><tt class="py-op">,</tt> <tt class="py-name">poll_timeout</tt><tt class="py-op">)</tt> </tt>
</div><a name="L256"></a><tt class="py-lineno">256</tt>  <tt class="py-line"> </tt>
<a name="ThreadPool.createWorkers"></a><div id="ThreadPool.createWorkers-def"><a name="L257"></a><tt class="py-lineno">257</tt> <a class="py-toggle" href="#" id="ThreadPool.createWorkers-toggle" onclick="return toggle('ThreadPool.createWorkers');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool.ThreadPool-class.html#createWorkers">createWorkers</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">num_workers</tt><tt class="py-op">,</tt> <tt class="py-param">poll_timeout</tt><tt class="py-op">=</tt><tt class="py-number">5</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="ThreadPool.createWorkers-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="ThreadPool.createWorkers-expanded"><a name="L258"></a><tt class="py-lineno">258</tt>  <tt class="py-line">        <tt class="py-docstring">"""Add num_workers worker threads to the pool.</tt> </tt>
<a name="L259"></a><tt class="py-lineno">259</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L260"></a><tt class="py-lineno">260</tt>  <tt class="py-line"><tt class="py-docstring">        ``poll_timout`` sets the interval in seconds (int or float) for how</tt> </tt>
<a name="L261"></a><tt class="py-lineno">261</tt>  <tt class="py-line"><tt class="py-docstring">        ofte threads should check whether they are dismissed, while waiting for</tt> </tt>
<a name="L262"></a><tt class="py-lineno">262</tt>  <tt class="py-line"><tt class="py-docstring">        requests.</tt> </tt>
<a name="L263"></a><tt class="py-lineno">263</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L264"></a><tt class="py-lineno">264</tt>  <tt class="py-line"><tt class="py-docstring">        """</tt> </tt>
<a name="L265"></a><tt class="py-lineno">265</tt>  <tt class="py-line">        <tt class="py-keyword">for</tt> <tt class="py-name">i</tt> <tt class="py-keyword">in</tt> <tt class="py-name">range</tt><tt class="py-op">(</tt><tt class="py-name">num_workers</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L266"></a><tt class="py-lineno">266</tt>  <tt class="py-line">            <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">workers</tt><tt class="py-op">.</tt><tt class="py-name">append</tt><tt class="py-op">(</tt><tt id="link-5" class="py-name" targets="Class threadpool.WorkerThread=threadpool.WorkerThread-class.html"><a title="threadpool.WorkerThread" class="py-name" href="#" onclick="return doclink('link-5', 'WorkerThread', 'link-5');">WorkerThread</a></tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_requests_queue</tt><tt class="py-op">,</tt> </tt>
<a name="L267"></a><tt class="py-lineno">267</tt>  <tt class="py-line">                <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_results_queue</tt><tt class="py-op">,</tt> <tt class="py-name">poll_timeout</tt><tt class="py-op">=</tt><tt class="py-name">poll_timeout</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> </tt>
</div><a name="L268"></a><tt class="py-lineno">268</tt>  <tt class="py-line"> </tt>
<a name="ThreadPool.dismissWorkers"></a><div id="ThreadPool.dismissWorkers-def"><a name="L269"></a><tt class="py-lineno">269</tt> <a class="py-toggle" href="#" id="ThreadPool.dismissWorkers-toggle" onclick="return toggle('ThreadPool.dismissWorkers');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool.ThreadPool-class.html#dismissWorkers">dismissWorkers</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">num_workers</tt><tt class="py-op">,</tt> <tt class="py-param">do_join</tt><tt class="py-op">=</tt><tt class="py-name">False</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="ThreadPool.dismissWorkers-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="ThreadPool.dismissWorkers-expanded"><a name="L270"></a><tt class="py-lineno">270</tt>  <tt class="py-line">        <tt class="py-docstring">"""Tell num_workers worker threads to quit after their current task."""</tt> </tt>
<a name="L271"></a><tt class="py-lineno">271</tt>  <tt class="py-line">        <tt class="py-name">dismiss_list</tt> <tt class="py-op">=</tt> <tt class="py-op">[</tt><tt class="py-op">]</tt> </tt>
<a name="L272"></a><tt class="py-lineno">272</tt>  <tt class="py-line">        <tt class="py-keyword">for</tt> <tt class="py-name">i</tt> <tt class="py-keyword">in</tt> <tt class="py-name">range</tt><tt class="py-op">(</tt><tt class="py-name">min</tt><tt class="py-op">(</tt><tt class="py-name">num_workers</tt><tt class="py-op">,</tt> <tt class="py-name">len</tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">workers</tt><tt class="py-op">)</tt><tt class="py-op">)</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L273"></a><tt class="py-lineno">273</tt>  <tt class="py-line">            <tt class="py-name">worker</tt> <tt class="py-op">=</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">workers</tt><tt class="py-op">.</tt><tt class="py-name">pop</tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
<a name="L274"></a><tt class="py-lineno">274</tt>  <tt class="py-line">            <tt class="py-name">worker</tt><tt class="py-op">.</tt><tt id="link-6" class="py-name" targets="Method threadpool.WorkerThread.dismiss()=threadpool.WorkerThread-class.html#dismiss"><a title="threadpool.WorkerThread.dismiss" class="py-name" href="#" onclick="return doclink('link-6', 'dismiss', 'link-6');">dismiss</a></tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
<a name="L275"></a><tt class="py-lineno">275</tt>  <tt class="py-line">            <tt class="py-name">dismiss_list</tt><tt class="py-op">.</tt><tt class="py-name">append</tt><tt class="py-op">(</tt><tt class="py-name">worker</tt><tt class="py-op">)</tt> </tt>
<a name="L276"></a><tt class="py-lineno">276</tt>  <tt class="py-line"> </tt>
<a name="L277"></a><tt class="py-lineno">277</tt>  <tt class="py-line">        <tt class="py-keyword">if</tt> <tt class="py-name">do_join</tt><tt class="py-op">:</tt> </tt>
<a name="L278"></a><tt class="py-lineno">278</tt>  <tt class="py-line">            <tt class="py-keyword">for</tt> <tt class="py-name">worker</tt> <tt class="py-keyword">in</tt> <tt class="py-name">dismiss_list</tt><tt class="py-op">:</tt> </tt>
<a name="L279"></a><tt class="py-lineno">279</tt>  <tt class="py-line">                <tt class="py-name">worker</tt><tt class="py-op">.</tt><tt class="py-name">join</tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
<a name="L280"></a><tt class="py-lineno">280</tt>  <tt class="py-line">        <tt class="py-keyword">else</tt><tt class="py-op">:</tt> </tt>
<a name="L281"></a><tt class="py-lineno">281</tt>  <tt class="py-line">            <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">dismissedWorkers</tt><tt class="py-op">.</tt><tt class="py-name">extend</tt><tt class="py-op">(</tt><tt class="py-name">dismiss_list</tt><tt class="py-op">)</tt> </tt>
</div><a name="L282"></a><tt class="py-lineno">282</tt>  <tt class="py-line"> </tt>
<a name="ThreadPool.joinAllDismissedWorkers"></a><div id="ThreadPool.joinAllDismissedWorkers-def"><a name="L283"></a><tt class="py-lineno">283</tt> <a class="py-toggle" href="#" id="ThreadPool.joinAllDismissedWorkers-toggle" onclick="return toggle('ThreadPool.joinAllDismissedWorkers');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool.ThreadPool-class.html#joinAllDismissedWorkers">joinAllDismissedWorkers</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="ThreadPool.joinAllDismissedWorkers-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="ThreadPool.joinAllDismissedWorkers-expanded"><a name="L284"></a><tt class="py-lineno">284</tt>  <tt class="py-line">        <tt class="py-docstring">"""Perform Thread.join() on all worker threads that have been dismissed.</tt> </tt>
<a name="L285"></a><tt class="py-lineno">285</tt>  <tt class="py-line"><tt class="py-docstring">        """</tt> </tt>
<a name="L286"></a><tt class="py-lineno">286</tt>  <tt class="py-line">        <tt class="py-keyword">for</tt> <tt class="py-name">worker</tt> <tt class="py-keyword">in</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">dismissedWorkers</tt><tt class="py-op">:</tt> </tt>
<a name="L287"></a><tt class="py-lineno">287</tt>  <tt class="py-line">            <tt class="py-name">worker</tt><tt class="py-op">.</tt><tt class="py-name">join</tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
<a name="L288"></a><tt class="py-lineno">288</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">dismissedWorkers</tt> <tt class="py-op">=</tt> <tt class="py-op">[</tt><tt class="py-op">]</tt> </tt>
</div><a name="L289"></a><tt class="py-lineno">289</tt>  <tt class="py-line"> </tt>
<a name="ThreadPool.putRequest"></a><div id="ThreadPool.putRequest-def"><a name="L290"></a><tt class="py-lineno">290</tt> <a class="py-toggle" href="#" id="ThreadPool.putRequest-toggle" onclick="return toggle('ThreadPool.putRequest');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool.ThreadPool-class.html#putRequest">putRequest</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">request</tt><tt class="py-op">,</tt> <tt class="py-param">block</tt><tt class="py-op">=</tt><tt class="py-name">True</tt><tt class="py-op">,</tt> <tt class="py-param">timeout</tt><tt class="py-op">=</tt><tt class="py-number">0</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="ThreadPool.putRequest-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="ThreadPool.putRequest-expanded"><a name="L291"></a><tt class="py-lineno">291</tt>  <tt class="py-line">        <tt class="py-docstring">"""Put work request into work queue and save its id for later."""</tt> </tt>
<a name="L292"></a><tt class="py-lineno">292</tt>  <tt class="py-line">        <tt class="py-keyword">assert</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">request</tt><tt class="py-op">,</tt> <tt id="link-7" class="py-name"><a title="threadpool.WorkRequest" class="py-name" href="#" onclick="return doclink('link-7', 'WorkRequest', 'link-1');">WorkRequest</a></tt><tt class="py-op">)</tt> </tt>
<a name="L293"></a><tt class="py-lineno">293</tt>  <tt class="py-line">        <tt class="py-comment"># don't reuse old work requests</tt> </tt>
<a name="L294"></a><tt class="py-lineno">294</tt>  <tt class="py-line"><tt class="py-comment"></tt>        <tt class="py-keyword">assert</tt> <tt class="py-keyword">not</tt> <tt class="py-name">getattr</tt><tt class="py-op">(</tt><tt class="py-name">request</tt><tt class="py-op">,</tt> <tt class="py-string">'exception'</tt><tt class="py-op">,</tt> <tt class="py-name">None</tt><tt class="py-op">)</tt> </tt>
<a name="L295"></a><tt class="py-lineno">295</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_requests_queue</tt><tt class="py-op">.</tt><tt class="py-name">put</tt><tt class="py-op">(</tt><tt class="py-name">request</tt><tt class="py-op">,</tt> <tt class="py-name">block</tt><tt class="py-op">,</tt> <tt class="py-name">timeout</tt><tt class="py-op">)</tt> </tt>
<a name="L296"></a><tt class="py-lineno">296</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">workRequests</tt><tt class="py-op">[</tt><tt class="py-name">request</tt><tt class="py-op">.</tt><tt class="py-name">requestID</tt><tt class="py-op">]</tt> <tt class="py-op">=</tt> <tt class="py-name">request</tt> </tt>
</div><a name="L297"></a><tt class="py-lineno">297</tt>  <tt class="py-line"> </tt>
<a name="ThreadPool.poll"></a><div id="ThreadPool.poll-def"><a name="L298"></a><tt class="py-lineno">298</tt> <a class="py-toggle" href="#" id="ThreadPool.poll-toggle" onclick="return toggle('ThreadPool.poll');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool.ThreadPool-class.html#poll">poll</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">block</tt><tt class="py-op">=</tt><tt class="py-name">False</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="ThreadPool.poll-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="ThreadPool.poll-expanded"><a name="L299"></a><tt class="py-lineno">299</tt>  <tt class="py-line">        <tt class="py-docstring">"""Process any new results in the queue."""</tt> </tt>
<a name="L300"></a><tt class="py-lineno">300</tt>  <tt class="py-line">        <tt class="py-keyword">while</tt> <tt class="py-name">True</tt><tt class="py-op">:</tt> </tt>
<a name="L301"></a><tt class="py-lineno">301</tt>  <tt class="py-line">            <tt class="py-comment"># still results pending?</tt> </tt>
<a name="L302"></a><tt class="py-lineno">302</tt>  <tt class="py-line"><tt class="py-comment"></tt>            <tt class="py-keyword">if</tt> <tt class="py-keyword">not</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">workRequests</tt><tt class="py-op">:</tt> </tt>
<a name="L303"></a><tt class="py-lineno">303</tt>  <tt class="py-line">                <tt class="py-keyword">raise</tt> <tt id="link-8" class="py-name" targets="Class threadpool.NoResultsPending=threadpool.NoResultsPending-class.html"><a title="threadpool.NoResultsPending" class="py-name" href="#" onclick="return doclink('link-8', 'NoResultsPending', 'link-8');">NoResultsPending</a></tt> </tt>
<a name="L304"></a><tt class="py-lineno">304</tt>  <tt class="py-line">            <tt class="py-comment"># are there still workers to process remaining requests?</tt> </tt>
<a name="L305"></a><tt class="py-lineno">305</tt>  <tt class="py-line"><tt class="py-comment"></tt>            <tt class="py-keyword">elif</tt> <tt class="py-name">block</tt> <tt class="py-keyword">and</tt> <tt class="py-keyword">not</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">workers</tt><tt class="py-op">:</tt> </tt>
<a name="L306"></a><tt class="py-lineno">306</tt>  <tt class="py-line">                <tt class="py-keyword">raise</tt> <tt id="link-9" class="py-name" targets="Class threadpool.NoWorkersAvailable=threadpool.NoWorkersAvailable-class.html"><a title="threadpool.NoWorkersAvailable" class="py-name" href="#" onclick="return doclink('link-9', 'NoWorkersAvailable', 'link-9');">NoWorkersAvailable</a></tt> </tt>
<a name="L307"></a><tt class="py-lineno">307</tt>  <tt class="py-line">            <tt class="py-keyword">try</tt><tt class="py-op">:</tt> </tt>
<a name="L308"></a><tt class="py-lineno">308</tt>  <tt class="py-line">                <tt class="py-comment"># get back next results</tt> </tt>
<a name="L309"></a><tt class="py-lineno">309</tt>  <tt class="py-line"><tt class="py-comment"></tt>                <tt class="py-name">request</tt><tt class="py-op">,</tt> <tt class="py-name">result</tt> <tt class="py-op">=</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">_results_queue</tt><tt class="py-op">.</tt><tt class="py-name">get</tt><tt class="py-op">(</tt><tt class="py-name">block</tt><tt class="py-op">=</tt><tt class="py-name">block</tt><tt class="py-op">)</tt> </tt>
<a name="L310"></a><tt class="py-lineno">310</tt>  <tt class="py-line">                <tt class="py-comment"># has an exception occured?</tt> </tt>
<a name="L311"></a><tt class="py-lineno">311</tt>  <tt class="py-line"><tt class="py-comment"></tt>                <tt class="py-keyword">if</tt> <tt class="py-name">request</tt><tt class="py-op">.</tt><tt class="py-name">exception</tt> <tt class="py-keyword">and</tt> <tt class="py-name">request</tt><tt class="py-op">.</tt><tt class="py-name">exc_callback</tt><tt class="py-op">:</tt> </tt>
<a name="L312"></a><tt class="py-lineno">312</tt>  <tt class="py-line">                    <tt class="py-name">request</tt><tt class="py-op">.</tt><tt class="py-name">exc_callback</tt><tt class="py-op">(</tt><tt class="py-name">request</tt><tt class="py-op">,</tt> <tt class="py-name">result</tt><tt class="py-op">)</tt> </tt>
<a name="L313"></a><tt class="py-lineno">313</tt>  <tt class="py-line">                <tt class="py-comment"># hand results to callback, if any</tt> </tt>
<a name="L314"></a><tt class="py-lineno">314</tt>  <tt class="py-line"><tt class="py-comment"></tt>                <tt class="py-keyword">if</tt> <tt class="py-name">request</tt><tt class="py-op">.</tt><tt class="py-name">callback</tt> <tt class="py-keyword">and</tt> <tt class="py-keyword">not</tt> \ </tt>
<a name="L315"></a><tt class="py-lineno">315</tt>  <tt class="py-line">                       <tt class="py-op">(</tt><tt class="py-name">request</tt><tt class="py-op">.</tt><tt class="py-name">exception</tt> <tt class="py-keyword">and</tt> <tt class="py-name">request</tt><tt class="py-op">.</tt><tt class="py-name">exc_callback</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L316"></a><tt class="py-lineno">316</tt>  <tt class="py-line">                    <tt class="py-name">request</tt><tt class="py-op">.</tt><tt class="py-name">callback</tt><tt class="py-op">(</tt><tt class="py-name">request</tt><tt class="py-op">,</tt> <tt class="py-name">result</tt><tt class="py-op">)</tt> </tt>
<a name="L317"></a><tt class="py-lineno">317</tt>  <tt class="py-line">                <tt class="py-keyword">del</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">workRequests</tt><tt class="py-op">[</tt><tt class="py-name">request</tt><tt class="py-op">.</tt><tt class="py-name">requestID</tt><tt class="py-op">]</tt> </tt>
<a name="L318"></a><tt class="py-lineno">318</tt>  <tt class="py-line">            <tt class="py-keyword">except</tt> <tt class="py-name">Queue</tt><tt class="py-op">.</tt><tt class="py-name">Empty</tt><tt class="py-op">:</tt> </tt>
<a name="L319"></a><tt class="py-lineno">319</tt>  <tt class="py-line">                <tt class="py-keyword">break</tt> </tt>
</div><a name="L320"></a><tt class="py-lineno">320</tt>  <tt class="py-line"> </tt>
<a name="ThreadPool.wait"></a><div id="ThreadPool.wait-def"><a name="L321"></a><tt class="py-lineno">321</tt> <a class="py-toggle" href="#" id="ThreadPool.wait-toggle" onclick="return toggle('ThreadPool.wait');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool.ThreadPool-class.html#wait">wait</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="ThreadPool.wait-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="ThreadPool.wait-expanded"><a name="L322"></a><tt class="py-lineno">322</tt>  <tt class="py-line">        <tt class="py-docstring">"""Wait for results, blocking until all have arrived."""</tt> </tt>
<a name="L323"></a><tt class="py-lineno">323</tt>  <tt class="py-line">        <tt class="py-keyword">while</tt> <tt class="py-number">1</tt><tt class="py-op">:</tt> </tt>
<a name="L324"></a><tt class="py-lineno">324</tt>  <tt class="py-line">            <tt class="py-keyword">try</tt><tt class="py-op">:</tt> </tt>
<a name="L325"></a><tt class="py-lineno">325</tt>  <tt class="py-line">                <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-10" class="py-name" targets="Method threadpool.ThreadPool.poll()=threadpool.ThreadPool-class.html#poll"><a title="threadpool.ThreadPool.poll" class="py-name" href="#" onclick="return doclink('link-10', 'poll', 'link-10');">poll</a></tt><tt class="py-op">(</tt><tt class="py-name">True</tt><tt class="py-op">)</tt> </tt>
<a name="L326"></a><tt class="py-lineno">326</tt>  <tt class="py-line">            <tt class="py-keyword">except</tt> <tt id="link-11" class="py-name"><a title="threadpool.NoResultsPending" class="py-name" href="#" onclick="return doclink('link-11', 'NoResultsPending', 'link-8');">NoResultsPending</a></tt><tt class="py-op">:</tt> </tt>
<a name="L327"></a><tt class="py-lineno">327</tt>  <tt class="py-line">                <tt class="py-keyword">break</tt> </tt>
</div></div><a name="L328"></a><tt class="py-lineno">328</tt>  <tt class="py-line"> </tt>
<a name="L329"></a><tt class="py-lineno">329</tt>  <tt class="py-line"> </tt>
<a name="L330"></a><tt class="py-lineno">330</tt>  <tt class="py-line"><tt class="py-comment">################</tt> </tt>
<a name="L331"></a><tt class="py-lineno">331</tt>  <tt class="py-line"><tt class="py-comment"></tt><tt class="py-comment"># USAGE EXAMPLE</tt> </tt>
<a name="L332"></a><tt class="py-lineno">332</tt>  <tt class="py-line"><tt class="py-comment"></tt><tt class="py-comment">################</tt> </tt>
<a name="L333"></a><tt class="py-lineno">333</tt>  <tt class="py-line"><tt class="py-comment"></tt> </tt>
<a name="L334"></a><tt class="py-lineno">334</tt>  <tt class="py-line"><tt class="py-keyword">if</tt> <tt class="py-name">__name__</tt> <tt class="py-op">==</tt> <tt class="py-string">'__main__'</tt><tt class="py-op">:</tt> </tt>
<a name="L335"></a><tt class="py-lineno">335</tt>  <tt class="py-line">    <tt class="py-keyword">import</tt> <tt class="py-name">random</tt> </tt>
<a name="L336"></a><tt class="py-lineno">336</tt>  <tt class="py-line">    <tt class="py-keyword">import</tt> <tt class="py-name">time</tt> </tt>
<a name="L337"></a><tt class="py-lineno">337</tt>  <tt class="py-line"> </tt>
<a name="L338"></a><tt class="py-lineno">338</tt>  <tt class="py-line">    <tt class="py-comment"># the work the threads will have to do (rather trivial in our example)</tt> </tt>
<a name="do_something"></a><div id="do_something-def"><a name="L339"></a><tt class="py-lineno">339</tt> <a class="py-toggle" href="#" id="do_something-toggle" onclick="return toggle('do_something');">-</a><tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool-module.html#do_something">do_something</a><tt class="py-op">(</tt><tt class="py-param">data</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="do_something-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="do_something-expanded"><a name="L340"></a><tt class="py-lineno">340</tt>  <tt class="py-line">        <tt class="py-name">time</tt><tt class="py-op">.</tt><tt class="py-name">sleep</tt><tt class="py-op">(</tt><tt class="py-name">random</tt><tt class="py-op">.</tt><tt class="py-name">randint</tt><tt class="py-op">(</tt><tt class="py-number">1</tt><tt class="py-op">,</tt><tt class="py-number">5</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> </tt>
<a name="L341"></a><tt class="py-lineno">341</tt>  <tt class="py-line">        <tt class="py-name">result</tt> <tt class="py-op">=</tt> <tt class="py-name">round</tt><tt class="py-op">(</tt><tt class="py-name">random</tt><tt class="py-op">.</tt><tt class="py-name">random</tt><tt class="py-op">(</tt><tt class="py-op">)</tt> <tt class="py-op">*</tt> <tt class="py-name">data</tt><tt class="py-op">,</tt> <tt class="py-number">5</tt><tt class="py-op">)</tt> </tt>
<a name="L342"></a><tt class="py-lineno">342</tt>  <tt class="py-line">        <tt class="py-comment"># just to show off, we throw an exception once in a while</tt> </tt>
<a name="L343"></a><tt class="py-lineno">343</tt>  <tt class="py-line"><tt class="py-comment"></tt>        <tt class="py-keyword">if</tt> <tt class="py-name">result</tt> <tt class="py-op">&gt;</tt> <tt class="py-number">5</tt><tt class="py-op">:</tt> </tt>
<a name="L344"></a><tt class="py-lineno">344</tt>  <tt class="py-line">            <tt class="py-keyword">raise</tt> <tt class="py-name">RuntimeError</tt><tt class="py-op">(</tt><tt class="py-string">"Something extraordinary happened!"</tt><tt class="py-op">)</tt> </tt>
<a name="L345"></a><tt class="py-lineno">345</tt>  <tt class="py-line">        <tt class="py-keyword">return</tt> <tt class="py-name">result</tt> </tt>
</div><a name="L346"></a><tt class="py-lineno">346</tt>  <tt class="py-line"> </tt>
<a name="L347"></a><tt class="py-lineno">347</tt>  <tt class="py-line">    <tt class="py-comment"># this will be called each time a result is available</tt> </tt>
<a name="print_result"></a><div id="print_result-def"><a name="L348"></a><tt class="py-lineno">348</tt> <a class="py-toggle" href="#" id="print_result-toggle" onclick="return toggle('print_result');">-</a><tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool-module.html#print_result">print_result</a><tt class="py-op">(</tt><tt class="py-param">request</tt><tt class="py-op">,</tt> <tt class="py-param">result</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="print_result-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="print_result-expanded"><a name="L349"></a><tt class="py-lineno">349</tt>  <tt class="py-line">        <tt class="py-keyword">print</tt> <tt class="py-string">"**** Result from request #%s: %r"</tt> <tt class="py-op">%</tt> <tt class="py-op">(</tt><tt class="py-name">request</tt><tt class="py-op">.</tt><tt class="py-name">requestID</tt><tt class="py-op">,</tt> <tt class="py-name">result</tt><tt class="py-op">)</tt> </tt>
</div><a name="L350"></a><tt class="py-lineno">350</tt>  <tt class="py-line"> </tt>
<a name="L351"></a><tt class="py-lineno">351</tt>  <tt class="py-line">    <tt class="py-comment"># this will be called when an exception occurs within a thread</tt> </tt>
<a name="L352"></a><tt class="py-lineno">352</tt>  <tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-comment"># this example exception handler does little more than the default handler</tt> </tt>
<a name="handle_exception"></a><div id="handle_exception-def"><a name="L353"></a><tt class="py-lineno">353</tt> <a class="py-toggle" href="#" id="handle_exception-toggle" onclick="return toggle('handle_exception');">-</a><tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-keyword">def</tt> <a class="py-def-name" href="threadpool-module.html#handle_exception">handle_exception</a><tt class="py-op">(</tt><tt class="py-param">request</tt><tt class="py-op">,</tt> <tt class="py-param">exc_info</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="handle_exception-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="handle_exception-expanded"><a name="L354"></a><tt class="py-lineno">354</tt>  <tt class="py-line">        <tt class="py-keyword">if</tt> <tt class="py-keyword">not</tt> <tt class="py-name">isinstance</tt><tt class="py-op">(</tt><tt class="py-name">exc_info</tt><tt class="py-op">,</tt> <tt class="py-name">tuple</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L355"></a><tt class="py-lineno">355</tt>  <tt class="py-line">            <tt class="py-comment"># Something is seriously wrong...</tt> </tt>
<a name="L356"></a><tt class="py-lineno">356</tt>  <tt class="py-line"><tt class="py-comment"></tt>            <tt class="py-keyword">print</tt> <tt class="py-name">request</tt> </tt>
<a name="L357"></a><tt class="py-lineno">357</tt>  <tt class="py-line">            <tt class="py-keyword">print</tt> <tt class="py-name">exc_info</tt> </tt>
<a name="L358"></a><tt class="py-lineno">358</tt>  <tt class="py-line">            <tt class="py-keyword">raise</tt> <tt class="py-name">SystemExit</tt> </tt>
<a name="L359"></a><tt class="py-lineno">359</tt>  <tt class="py-line">        <tt class="py-keyword">print</tt> <tt class="py-string">"**** Exception occured in request #%s: %s"</tt> <tt class="py-op">%</tt> \ </tt>
<a name="L360"></a><tt class="py-lineno">360</tt>  <tt class="py-line">          <tt class="py-op">(</tt><tt class="py-name">request</tt><tt class="py-op">.</tt><tt class="py-name">requestID</tt><tt class="py-op">,</tt> <tt class="py-name">exc_info</tt><tt class="py-op">)</tt> </tt>
</div><a name="L361"></a><tt class="py-lineno">361</tt>  <tt class="py-line"> </tt>
<a name="L362"></a><tt class="py-lineno">362</tt>  <tt class="py-line">    <tt class="py-comment"># assemble the arguments for each job to a list...</tt> </tt>
<a name="L363"></a><tt class="py-lineno">363</tt>  <tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-name">data</tt> <tt class="py-op">=</tt> <tt class="py-op">[</tt><tt class="py-name">random</tt><tt class="py-op">.</tt><tt class="py-name">randint</tt><tt class="py-op">(</tt><tt class="py-number">1</tt><tt class="py-op">,</tt><tt class="py-number">10</tt><tt class="py-op">)</tt> <tt class="py-keyword">for</tt> <tt class="py-name">i</tt> <tt class="py-keyword">in</tt> <tt class="py-name">range</tt><tt class="py-op">(</tt><tt class="py-number">20</tt><tt class="py-op">)</tt><tt class="py-op">]</tt> </tt>
<a name="L364"></a><tt class="py-lineno">364</tt>  <tt class="py-line">    <tt class="py-comment"># ... and build a WorkRequest object for each item in data</tt> </tt>
<a name="L365"></a><tt class="py-lineno">365</tt>  <tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-name">requests</tt> <tt class="py-op">=</tt> <tt id="link-12" class="py-name" targets="Function threadpool.makeRequests()=threadpool-module.html#makeRequests"><a title="threadpool.makeRequests" class="py-name" href="#" onclick="return doclink('link-12', 'makeRequests', 'link-12');">makeRequests</a></tt><tt class="py-op">(</tt><tt class="py-name">do_something</tt><tt class="py-op">,</tt> <tt class="py-name">data</tt><tt class="py-op">,</tt> <tt class="py-name">print_result</tt><tt class="py-op">,</tt> <tt class="py-name">handle_exception</tt><tt class="py-op">)</tt> </tt>
<a name="L366"></a><tt class="py-lineno">366</tt>  <tt class="py-line">    <tt class="py-comment"># to use the default exception handler, uncomment next line and comment out</tt> </tt>
<a name="L367"></a><tt class="py-lineno">367</tt>  <tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-comment"># the preceding one.</tt> </tt>
<a name="L368"></a><tt class="py-lineno">368</tt>  <tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-comment">#requests = makeRequests(do_something, data, print_result)</tt> </tt>
<a name="L369"></a><tt class="py-lineno">369</tt>  <tt class="py-line"><tt class="py-comment"></tt> </tt>
<a name="L370"></a><tt class="py-lineno">370</tt>  <tt class="py-line">    <tt class="py-comment"># or the other form of args_lists accepted by makeRequests: ((,), {})</tt> </tt>
<a name="L371"></a><tt class="py-lineno">371</tt>  <tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-name">data</tt> <tt class="py-op">=</tt> <tt class="py-op">[</tt><tt class="py-op">(</tt><tt class="py-op">(</tt><tt class="py-name">random</tt><tt class="py-op">.</tt><tt class="py-name">randint</tt><tt class="py-op">(</tt><tt class="py-number">1</tt><tt class="py-op">,</tt><tt class="py-number">10</tt><tt class="py-op">)</tt><tt class="py-op">,</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-op">{</tt><tt class="py-op">}</tt><tt class="py-op">)</tt> <tt class="py-keyword">for</tt> <tt class="py-name">i</tt> <tt class="py-keyword">in</tt> <tt class="py-name">range</tt><tt class="py-op">(</tt><tt class="py-number">20</tt><tt class="py-op">)</tt><tt class="py-op">]</tt> </tt>
<a name="L372"></a><tt class="py-lineno">372</tt>  <tt class="py-line">    <tt class="py-name">requests</tt><tt class="py-op">.</tt><tt class="py-name">extend</tt><tt class="py-op">(</tt> </tt>
<a name="L373"></a><tt class="py-lineno">373</tt>  <tt class="py-line">        <tt id="link-13" class="py-name"><a title="threadpool.makeRequests" class="py-name" href="#" onclick="return doclink('link-13', 'makeRequests', 'link-12');">makeRequests</a></tt><tt class="py-op">(</tt><tt class="py-name">do_something</tt><tt class="py-op">,</tt> <tt class="py-name">data</tt><tt class="py-op">,</tt> <tt class="py-name">print_result</tt><tt class="py-op">,</tt> <tt class="py-name">handle_exception</tt><tt class="py-op">)</tt> </tt>
<a name="L374"></a><tt class="py-lineno">374</tt>  <tt class="py-line">        <tt class="py-comment">#makeRequests(do_something, data, print_result)</tt> </tt>
<a name="L375"></a><tt class="py-lineno">375</tt>  <tt class="py-line">        <tt class="py-comment"># to use the default exception handler, uncomment next line and comment</tt> </tt>
<a name="L376"></a><tt class="py-lineno">376</tt>  <tt class="py-line">        <tt class="py-comment"># out the preceding one.</tt> </tt>
<a name="L377"></a><tt class="py-lineno">377</tt>  <tt class="py-line">    <tt class="py-op">)</tt> </tt>
<a name="L378"></a><tt class="py-lineno">378</tt>  <tt class="py-line"> </tt>
<a name="L379"></a><tt class="py-lineno">379</tt>  <tt class="py-line">    <tt class="py-comment"># we create a pool of 3 worker threads</tt> </tt>
<a name="L380"></a><tt class="py-lineno">380</tt>  <tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-keyword">print</tt> <tt class="py-string">"Creating thread pool with 3 worker threads."</tt> </tt>
<a name="L381"></a><tt class="py-lineno">381</tt>  <tt class="py-line">    <tt class="py-name">main</tt> <tt class="py-op">=</tt> <tt id="link-14" class="py-name" targets="Class threadpool.ThreadPool=threadpool.ThreadPool-class.html"><a title="threadpool.ThreadPool" class="py-name" href="#" onclick="return doclink('link-14', 'ThreadPool', 'link-14');">ThreadPool</a></tt><tt class="py-op">(</tt><tt class="py-number">3</tt><tt class="py-op">)</tt> </tt>
<a name="L382"></a><tt class="py-lineno">382</tt>  <tt class="py-line"> </tt>
<a name="L383"></a><tt class="py-lineno">383</tt>  <tt class="py-line">    <tt class="py-comment"># then we put the work requests in the queue...</tt> </tt>
<a name="L384"></a><tt class="py-lineno">384</tt>  <tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-keyword">for</tt> <tt class="py-name">req</tt> <tt class="py-keyword">in</tt> <tt class="py-name">requests</tt><tt class="py-op">:</tt> </tt>
<a name="L385"></a><tt class="py-lineno">385</tt>  <tt class="py-line">        <tt class="py-name">main</tt><tt class="py-op">.</tt><tt id="link-15" class="py-name" targets="Method threadpool.ThreadPool.putRequest()=threadpool.ThreadPool-class.html#putRequest"><a title="threadpool.ThreadPool.putRequest" class="py-name" href="#" onclick="return doclink('link-15', 'putRequest', 'link-15');">putRequest</a></tt><tt class="py-op">(</tt><tt class="py-name">req</tt><tt class="py-op">)</tt> </tt>
<a name="L386"></a><tt class="py-lineno">386</tt>  <tt class="py-line">        <tt class="py-keyword">print</tt> <tt class="py-string">"Work request #%s added."</tt> <tt class="py-op">%</tt> <tt class="py-name">req</tt><tt class="py-op">.</tt><tt class="py-name">requestID</tt> </tt>
<a name="L387"></a><tt class="py-lineno">387</tt>  <tt class="py-line">    <tt class="py-comment"># or shorter:</tt> </tt>
<a name="L388"></a><tt class="py-lineno">388</tt>  <tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-comment"># [main.putRequest(req) for req in requests]</tt> </tt>
<a name="L389"></a><tt class="py-lineno">389</tt>  <tt class="py-line"><tt class="py-comment"></tt> </tt>
<a name="L390"></a><tt class="py-lineno">390</tt>  <tt class="py-line">    <tt class="py-comment"># ...and wait for the results to arrive in the result queue</tt> </tt>
<a name="L391"></a><tt class="py-lineno">391</tt>  <tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-comment"># by using ThreadPool.wait(). This would block until results for</tt> </tt>
<a name="L392"></a><tt class="py-lineno">392</tt>  <tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-comment"># all work requests have arrived:</tt> </tt>
<a name="L393"></a><tt class="py-lineno">393</tt>  <tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-comment"># main.wait()</tt> </tt>
<a name="L394"></a><tt class="py-lineno">394</tt>  <tt class="py-line"><tt class="py-comment"></tt> </tt>
<a name="L395"></a><tt class="py-lineno">395</tt>  <tt class="py-line">    <tt class="py-comment"># instead we can poll for results while doing something else:</tt> </tt>
<a name="L396"></a><tt class="py-lineno">396</tt>  <tt class="py-line"><tt class="py-comment"></tt>    <tt class="py-name">i</tt> <tt class="py-op">=</tt> <tt class="py-number">0</tt> </tt>
<a name="L397"></a><tt class="py-lineno">397</tt>  <tt class="py-line">    <tt class="py-keyword">while</tt> <tt class="py-name">True</tt><tt class="py-op">:</tt> </tt>
<a name="L398"></a><tt class="py-lineno">398</tt>  <tt class="py-line">        <tt class="py-keyword">try</tt><tt class="py-op">:</tt> </tt>
<a name="L399"></a><tt class="py-lineno">399</tt>  <tt class="py-line">            <tt class="py-name">time</tt><tt class="py-op">.</tt><tt class="py-name">sleep</tt><tt class="py-op">(</tt><tt class="py-number">0.5</tt><tt class="py-op">)</tt> </tt>
<a name="L400"></a><tt class="py-lineno">400</tt>  <tt class="py-line">            <tt class="py-name">main</tt><tt class="py-op">.</tt><tt id="link-16" class="py-name"><a title="threadpool.ThreadPool.poll" class="py-name" href="#" onclick="return doclink('link-16', 'poll', 'link-10');">poll</a></tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
<a name="L401"></a><tt class="py-lineno">401</tt>  <tt class="py-line">            <tt class="py-keyword">print</tt> <tt class="py-string">"Main thread working..."</tt><tt class="py-op">,</tt> </tt>
<a name="L402"></a><tt class="py-lineno">402</tt>  <tt class="py-line">            <tt class="py-keyword">print</tt> <tt class="py-string">"(active worker threads: %i)"</tt> <tt class="py-op">%</tt> <tt class="py-op">(</tt><tt class="py-name">threading</tt><tt class="py-op">.</tt><tt class="py-name">activeCount</tt><tt class="py-op">(</tt><tt class="py-op">)</tt><tt class="py-op">-</tt><tt class="py-number">1</tt><tt class="py-op">,</tt> <tt class="py-op">)</tt> </tt>
<a name="L403"></a><tt class="py-lineno">403</tt>  <tt class="py-line">            <tt class="py-keyword">if</tt> <tt class="py-name">i</tt> <tt class="py-op">==</tt> <tt class="py-number">10</tt><tt class="py-op">:</tt> </tt>
<a name="L404"></a><tt class="py-lineno">404</tt>  <tt class="py-line">                <tt class="py-keyword">print</tt> <tt class="py-string">"**** Adding 3 more worker threads..."</tt> </tt>
<a name="L405"></a><tt class="py-lineno">405</tt>  <tt class="py-line">                <tt class="py-name">main</tt><tt class="py-op">.</tt><tt id="link-17" class="py-name"><a title="threadpool.ThreadPool.createWorkers" class="py-name" href="#" onclick="return doclink('link-17', 'createWorkers', 'link-4');">createWorkers</a></tt><tt class="py-op">(</tt><tt class="py-number">3</tt><tt class="py-op">)</tt> </tt>
<a name="L406"></a><tt class="py-lineno">406</tt>  <tt class="py-line">            <tt class="py-keyword">if</tt> <tt class="py-name">i</tt> <tt class="py-op">==</tt> <tt class="py-number">20</tt><tt class="py-op">:</tt> </tt>
<a name="L407"></a><tt class="py-lineno">407</tt>  <tt class="py-line">                <tt class="py-keyword">print</tt> <tt class="py-string">"**** Dismissing 2 worker threads..."</tt> </tt>
<a name="L408"></a><tt class="py-lineno">408</tt>  <tt class="py-line">                <tt class="py-name">main</tt><tt class="py-op">.</tt><tt id="link-18" class="py-name" targets="Method threadpool.ThreadPool.dismissWorkers()=threadpool.ThreadPool-class.html#dismissWorkers"><a title="threadpool.ThreadPool.dismissWorkers" class="py-name" href="#" onclick="return doclink('link-18', 'dismissWorkers', 'link-18');">dismissWorkers</a></tt><tt class="py-op">(</tt><tt class="py-number">2</tt><tt class="py-op">)</tt> </tt>
<a name="L409"></a><tt class="py-lineno">409</tt>  <tt class="py-line">            <tt class="py-name">i</tt> <tt class="py-op">+=</tt> <tt class="py-number">1</tt> </tt>
<a name="L410"></a><tt class="py-lineno">410</tt>  <tt class="py-line">        <tt class="py-keyword">except</tt> <tt class="py-name">KeyboardInterrupt</tt><tt class="py-op">:</tt> </tt>
<a name="L411"></a><tt class="py-lineno">411</tt>  <tt class="py-line">            <tt class="py-keyword">print</tt> <tt class="py-string">"**** Interrupted!"</tt> </tt>
<a name="L412"></a><tt class="py-lineno">412</tt>  <tt class="py-line">            <tt class="py-keyword">break</tt> </tt>
<a name="L413"></a><tt class="py-lineno">413</tt>  <tt class="py-line">        <tt class="py-keyword">except</tt> <tt id="link-19" class="py-name"><a title="threadpool.NoResultsPending" class="py-name" href="#" onclick="return doclink('link-19', 'NoResultsPending', 'link-8');">NoResultsPending</a></tt><tt class="py-op">:</tt> </tt>
<a name="L414"></a><tt class="py-lineno">414</tt>  <tt class="py-line">            <tt class="py-keyword">print</tt> <tt class="py-string">"**** No pending results."</tt> </tt>
<a name="L415"></a><tt class="py-lineno">415</tt>  <tt class="py-line">            <tt class="py-keyword">break</tt> </tt>
<a name="L416"></a><tt class="py-lineno">416</tt>  <tt class="py-line">    <tt class="py-keyword">if</tt> <tt class="py-name">main</tt><tt class="py-op">.</tt><tt class="py-name">dismissedWorkers</tt><tt class="py-op">:</tt> </tt>
<a name="L417"></a><tt class="py-lineno">417</tt>  <tt class="py-line">        <tt class="py-keyword">print</tt> <tt class="py-string">"Joining all dismissed worker threads..."</tt> </tt>
<a name="L418"></a><tt class="py-lineno">418</tt>  <tt class="py-line">        <tt class="py-name">main</tt><tt class="py-op">.</tt><tt id="link-20" class="py-name" targets="Method threadpool.ThreadPool.joinAllDismissedWorkers()=threadpool.ThreadPool-class.html#joinAllDismissedWorkers"><a title="threadpool.ThreadPool.joinAllDismissedWorkers" class="py-name" href="#" onclick="return doclink('link-20', 'joinAllDismissedWorkers', 'link-20');">joinAllDismissedWorkers</a></tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
<a name="L419"></a><tt class="py-lineno">419</tt>  <tt class="py-line"> </tt><script type="text/javascript">
<!--
expandto(location.href);
// -->
</script>
</pre>
<br />
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th bgcolor="#70b0f0" class="navbar-select"
          >&nbsp;&nbsp;&nbsp;Home&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="http://chrisarndt.de/projects/threadpool/">Threadpool</a></th>
          </tr></table></th>
  </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
  <tr>
    <td align="left" class="footer">
    Generated by Epydoc 3.0.1 on Wed Nov 19 18:43:47 2008
    </td>
    <td align="right" class="footer">
      <a target="mainFrame" href="http://epydoc.sourceforge.net"
        >http://epydoc.sourceforge.net</a>
    </td>
  </tr>
</table>

<script type="text/javascript">
  <!--
  // Private objects are initially displayed (because if
  // javascript is turned off then we want them to be
  // visible); but by default, we want to hide them.  So hide
  // them unless we have a cookie that says to show them.
  checkCookie();
  // -->
</script>
</body>
</html>
